<?php
	
	class mysqlDB{
		public $host;
		public $port;
		public $name;
		public $password;
		public $charset;
        public $dbName;
        private static $link;
        private $result;

        public static function getVideoData($config){
            if(!isset(self::$link)){
                self::$link = new self($config);
            }
            return self::$link;
        }
        //单例模式
		private function __construct($config){
            // echo $config['host'];
            $this->host = isset($config['host'])?$config['host']:localhost;
            $this->port = isset($config['port'])?$config['port']:3306;
            $this->name = isset($config['name'])?$config['name']:root;
            $this->password = isset($config['password'])?$config['password']:root;
            $this->charset = isset($config['charset'])?$config['charset']:"utf8";
            $this->dbName = isset($config['dbName'])?$config['dbName']:"admin";
            
            self::$link = $this->connect($this->host,$this->port,$this->name,$this->password);
             
            $this->setCharset($this->charset);
            $this->setSelect($this->dbName);
            // echo "数据库连success";
         
        }
        //数据库连接
        public function connect($host,$port,$name,$password){
            $this->result = mysqli_connect("$host:$port","$name","$password") or die("数据库连接失败");
            
        }
        //设置编码
        public function setCharset($charset){
            mysqli_set_charset($this->result,$charset);
        }
        //选择数据库
        public function setSelect($dbName){
            mysqli_select_db($this->result,$dbName);
        }

        //执行最简单的数据库查询结果集
        public function getJieguo($sql){
            $ret = mysqli_query($this->result,$sql);
            if(!$ret){
                echo "执行失败。<br>";
                echo "失败的sql语句为".$sql."<br>";
                echo "出错信息为：".mysqli_error($this->result)."<br>";
                echo "错误编号为：".mysqli_errno($this->result);
                die();
            }
            return $ret;
        }

        //返回多条数据
        public function getAll($table_name,$xiangmu=array()){
            $arr = array();
            $ss = "*";
            if(count($xiangmu)>0){
                $ss = implode(',',$xiangmu);
            }
            $sql = "select $ss from $table_name";
            $ret = $this->getJieguo($sql);
            while($row = mysqli_fetch_assoc($ret)){
                array_push($arr,$row);
            }
            return $arr;
        }

        //返回单条数据
        public function getOne($sql){
            $ret = $this->getJieguo($sql);
            $row = mysqli_fetch_assoc($ret);
            return $row;
        }

        //插入数据
        public function insert($table_name,$data){
            $keys = [];
            $values = [];

            foreach($data as $key=>$value){
                $keys[] = $key;
                if(is_numeric($value)){
                    $values[] = $value;
                }
                else{
                    $values[] = '"'.$value.'"';
                }
            }
            $key_str = '(' . implode(",", $keys) . ')';
            $value_str = '(' . implode(",", $values) . ')';
            $sql = "INSERT INTO {$table_name} {$key_str}  VALUES  {$value_str}";
            
            $ret = $this->getJieguo($sql);
            return $ret;

        }

        //把字符串变json格式
        public function JSON($code,$data=array()){
            if(!is_numeric($code)){
                return '';
            }
            
            $result = array(
                "code"=>$code,
                "data"=>$data
            );
            return json_encode($result,JSON_UNESCAPED_UNICODE);

        }

        

        //关闭数据库
        function closeDb(){
            mysqli_close($this->result);
        }

	}

    
    




?>